package com.xms.schema.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.core.exception.XmsException;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.sdk.core.query.OrderExpression;
import com.xms.sdk.core.query.OrderType;
import com.xms.sdk.service.IDataFinder;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("{org}/api/schema/entitytree")
public class EntityTreeController extends ApiBaseController {
    private IEntityFinderService _entityFinderService;
    private IDataFinder _dataFinder;
    @Autowired
    public EntityTreeController(IEntityFinderService entityFinderService, IDataFinder dataFinder)
    {
        _entityFinderService = entityFinderService;
        _dataFinder = dataFinder;
    }

    @ApiOperation("实体列表-JSON树格式")
    @GetMapping(value="")
    public JsonResultObject get() throws XmsException, SQLException {
        //return JOk();
        LambdaQueryWrapper<Entity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Entity::getEntityid, Entity::getName, Entity::getLocalizedname, Entity::getEntitygroups)
        .orderByAsc(Entity::getLocalizedname);
        List<Entity> entities = _entityFinderService.list(queryWrapper);
        List<com.xms.core.data.Entity> groups = _dataFinder.retrieveAll("entitygroup", Arrays.asList("name"), new OrderExpression("name", OrderType.Ascending));
        if (entities.stream().anyMatch(x -> StringUtil.isEmpty(x.getEntitygroups())))
        {
            com.xms.core.data.Entity nullGroup = new com.xms.core.data.Entity("entitygroup");
            nullGroup.setIdValue(UUIDUtil.Empty);
            nullGroup.setValue("name", "未分组");
            groups.add(0, nullGroup);
            for (Entity item : entities.stream().filter(x -> StringUtil.isEmpty(x.getEntitygroups())).collect(Collectors.toList()))
            {
                item.setEntitygroups(UUIDUtil.Empty.toString());
            }
        }
        List<HashMap<String, Object>> result = new ArrayList<>();
        for (com.xms.core.data.Entity group : groups)
        {
            HashMap<String, Object> g = new HashMap<>();
            g.put("label", group.getValue("name"));
            g.put("id", group.getIdValue());
            List<HashMap<String, Object>> childrens = new ArrayList<>();
            entities.stream()
                    .filter(x -> StringUtil.notEmpty(x.getEntitygroups())
                            && x.getEntitygroups().indexOf(group.getIdValue().toString()) >= 0).forEach(f->{
                HashMap<String, Object> map = new HashMap<>();
                map.put("label", f.getLocalizedname());
                map.put("id", f.getEntityid());
                childrens.add(map);
            });
            g.put("children", childrens);
            result.add(g); ;
        }

        return JOk(result);
    }
}